<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="初训证书机构" prop="firstCertOrganization">
        <el-select v-model="queryParams.firstCertOrganization" placeholder="请选择初训证书机构">
          <el-option v-for="item in cert_organizationList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="初训证书名称" prop="firstCertOrganization">
        <el-select v-model="queryParams.firstCertType" placeholder="请选择初训证书名称">
          <el-option v-for="item in cert_typeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="复训证书机构" prop="reCertOrganization">
        <el-select v-model="queryParams.reCertOrganization" placeholder="请选择复训证书机构">
          <el-option v-for="item in cert_organizationList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="复训证书名称" prop="firstCertOrganization">
        <el-select v-model="queryParams.reCertType" placeholder="请选择复训证书名称">
          <el-option v-for="item in cert_typeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:class_score:add']">新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['system:class_score:remove']">删除</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="class_scoreList" @selection-change="handleSelectionChange" :cell-class-name="tableRowStyle">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="班级" align="center" prop="trainingClass.name" width="100px"/>
      <el-table-column label="初训考试分值" align="center" prop="firstExScore" width="100px"/>
      <el-table-column label="初训评分表分值" align="center" prop="firstTableScore" width="115px"/>
      <el-table-column label="初训证书机构" align="center" prop="firstCertOrganizationBean.name"/>
      <el-table-column label="初训证书名称" align="center" prop="firstCertTypeBean.name"/>
      <el-table-column label="复训考试分值" align="center" prop="reExScore" width="100px"/>
      <el-table-column label="复训评分表分值" align="center" prop="reTableScore" width="115px"/>
      <el-table-column label="复训证书机构" align="center" prop="reCertOrganizationBean.name"/>
      <el-table-column label="复训证书名称" align="center" prop="reCertTypeBean.name" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:class_score:edit']">修改</el-button>
<!--          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:class_score:remove']">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改培训班分值对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="初训考试分值" prop="firstExScore">
          <el-input-number v-model="form.firstExScore" placeholder="请输入初训考试分值" controls-position="right" class="input-w-100" :min="0"/>
        </el-form-item>
        <el-form-item label="初训评分表分值" prop="firstTableScore">
          <el-input-number v-model="form.firstTableScore" placeholder="请输入初训评分表分值" controls-position="right" class="input-w-100" :min="0"/>
        </el-form-item>
        <el-form-item label="初训证书机构" prop="firstCertOrganization">
          <el-select v-model="form.firstCertOrganization" placeholder="请选择">
            <el-option v-for="item in cert_organizationList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="初训证书名称" prop="firstCertType">
          <el-select v-model="form.firstCertType" placeholder="请选择">
            <el-option v-for="item in cert_typeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="复训考试分值" prop="reExScore">
          <el-input-number v-model="form.reExScore" placeholder="请输入复训考试分值" controls-position="right" class="input-w-100" :min="0"/>
        </el-form-item>
        <el-form-item label="复训评分表分值" prop="reTableScore">
          <el-input-number v-model="form.reTableScore" placeholder="请输入复训评分表分值" controls-position="right" class="input-w-100" :min="0"/>
        </el-form-item>
        <el-form-item label="复训证书机构" prop="reCertOrganization">
          <el-select v-model="form.reCertOrganization" placeholder="请选择复训证书机构">
            <el-option v-for="item in cert_organizationList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="复训证书名称" prop="reCertType">
          <el-select v-model="form.reCertType" placeholder="请选择">
            <el-option v-for="item in cert_typeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listClass_score,
  getClass_score,
  delClass_score,
  addClass_score,
  updateClass_score
} from "@/api/pv/class_score";
import {listCert_organization,} from "@/api/cert/cert_organization";
import {listCert_type} from "@/api/cert/cert_type";

import {containAny} from "@/utils/zzDouUtils/arrayUtil";

export default {
  name: "Class_score",
  data() {
    return {
      // 遮罩层
      loading: true,
      cert_organizationList:[],
      cert_typeList:[],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 培训班分值表格数据
      class_scoreList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        firstExScore: null,
        firstTableScore: null,
        firstCertOrganization: null,
        firstCertType: null,
        reExScore: null,
        reTableScore: null,
        reCertOrganization: null,
        reCertType: null,
        reservedOne: null,
        reservedTwo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.getCertOrganizationList();
    this.getCertTypeList();
  },
  methods: {
    /** 查询证书类型列表 */
    getCertTypeList() {
      listCert_type({pageNum: 1,pageSize: 1000}).then(response => {
        this.cert_typeList = response.rows;
      });
    },
    /** 查询颁发机构列表 */
    getCertOrganizationList() {
      listCert_organization({pageNum: 1,pageSize: 1000}).then(response => {
        this.cert_organizationList = response.rows;
      });
    },
    /** 查询培训班分值列表 */
    getList() {
      this.loading = true;
      listClass_score(this.queryParams).then(response => {
        this.class_scoreList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        firstExScore: null,
        firstTableScore: null,
        firstCertOrganization: null,
        firstCertType: null,
        reExScore: null,
        reTableScore: null,
        reCertOrganization: null,
        reCertType: null,
        reservedOne: null,
        reservedTwo: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加培训班分值";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getClass_score(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改培训班分值";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateClass_score(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClass_score(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除培训班分值编号为"' + ids + '"的数据项？').then(function () {
        return delClass_score(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/class_score/export', {
        ...this.queryParams
      }, `class_score_${new Date().getTime()}.xlsx`)
    },
    tableRowStyle({row, column, rowIndex, columnIndex}){
      if (containAny(columnIndex,2,3,4,5)) {
        return 'warning-row';
      }else if (containAny(columnIndex,6,7,8,9)){
        return 'success-row';
      }
    }
  }
};
</script>
<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>
